package com.bootdo.timing.controller;

import java.math.BigDecimal;
import java.util.*;

import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.*;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserRoleDO;
import com.bootdo.system.service.RoleService;
import com.bootdo.system.service.UserRoleService;
import com.bootdo.system.service.UserService;
import com.bootdo.timing.domain.CountDO;
import com.bootdo.timing.dto.CombCountDto;
import com.bootdo.timing.dto.CountDto;
import ma.glasnost.orika.MapperFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.velocity.runtime.directive.Foreach;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.timing.domain.CombCountDO;
import com.bootdo.timing.service.CombCountService;

/**
 * 
 * 
 * @author caisj
 * @email 1992lcg@163.com
 * @date 2020-01-08 16:36:52
 */
 
@Controller
@RequestMapping("/timing/combCount")
public class CombCountController {
	@Autowired
	private CombCountService combCountService;

	@Autowired
	private MapperFactory mapperFactory;

	@Autowired
	private UserService userService;

	@Autowired
	private RedisUtils redisUtils;

	@Autowired
	private RoleService roleService;


	@Autowired
	private UserRoleService userRoleService;

	@GetMapping()
	@RequiresPermissions("timing:combCount:combCount")
	String CombCount() {
		return "timing/combCount/combCount";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("timing:combCount:combCount")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);
		if (1==2 ){


		List<CombCountDO> combCountList = combCountService.list(query);
		int total = combCountService.count(query);

		combCountList.forEach(combCountDO -> {
			try {
				CombCountDto dto = mapperFactory.getMapperFacade(CombCountDO.class, CombCountDto.class).map(combCountDO);
				//市场价格   购买数量 * 购买市场价格 ，

				BigDecimal buyTotalNum = combCountDO.getBuyTotalNum();

				BigDecimal buyTotalPrice = combCountDO.getBuyTotalPrice();

				String marktSinglePrice = (String) redisUtils.get(Constant.SYMBOL_PRICE_KEY + combCountDO.getCurrencyType());
				String marktTotalPrice = CalculationUtil.multiply(String.valueOf(buyTotalNum), marktSinglePrice, 4);
				//	// （市场价格-购买价格） / 购买价格  市场比例
				String priceSubtract = CalculationUtil.subtract(marktTotalPrice, String.valueOf(buyTotalPrice), 4);
				String profitSacle = CalculationUtil.divide(priceSubtract, String.valueOf(buyTotalPrice), 2);
				dto.setProfitSacle(new BigDecimal(profitSacle));
				dto.setMarktTotalPrice(new BigDecimal(marktTotalPrice));
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 购买的总数量
		});

		}
		
		List<CombCountDO> combCountList = new ArrayList<>();
		for(int i = 0; i < 5; i++){
			CombCountDO combCountDO = new CombCountDO();
			combCountDO.setId((long)(i+1));
			combCountDO.setUserId((long)(i+10));
			combCountDO.setCombId((long)(i+20));
			combCountDO.setCountTime(new Date());
			combCountDO.setCurrencyType("eosusdt" + i);
			combCountDO.setBuyTotalPrice(new BigDecimal(200 + i));
			combCountDO.setBuyTotalNum(new BigDecimal(0.4423 + i));
			combCountList.add(combCountDO);
		}
		
		PageUtils pageUtils = new PageUtils(combCountList, 1);
		return pageUtils;
	}
	
	@ResponseBody
	@GetMapping("/getCount")
	@RequiresPermissions("timing:combCount:getCount")
	public PageUtils getCount(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);
		
		if ( 1==2 ){
			
			query.put("roleId" , "59");
			List<CountDto> list = new ArrayList<>();
			try {
				List<UserRoleDO> userRoles = userRoleService.list(query);
				for (UserRoleDO userRole : userRoles) {
					UserDO userDO = userService.get(userRole.getUserId());
					query.remove("roleId");
					query.put("userId",userRole.getUserId());
					List<CombCountDO> combCountList = combCountService.list(query);
					String mtp = "0"; // 市场价格
					String s = "0"; // 比例
					String buyp = "";//购买价格
					for (CombCountDO combCountDO : combCountList) {
						if (combCountDO.getBuyTotalNum().compareTo(BigDecimal.ZERO) == 0  ){ // 购买数量大于 0
							//市场价格   购买数量 * 购买市场价格
							String buyTotalNum = CalculationUtil.add(String.valueOf(combCountDO.getBuyTotalNum()),String.valueOf(combCountDO.getBuyTotalNum()));
							BigDecimal buyTotalPrice = combCountDO.getBuyTotalPrice();
							String marktSinglePrice = (String) redisUtils.get(Constant.SYMBOL_PRICE_KEY + combCountDO.getCurrencyType());
							String marktTotalPrice = CalculationUtil.multiply(String.valueOf(buyTotalNum), marktSinglePrice, 4);
							//	// （市场总价格 - 购买价格 ） / 购买价格  市场比例
							String priceSubtract = CalculationUtil.subtract(marktTotalPrice, String.valueOf(buyTotalPrice), 4);
							String scale   = CalculationUtil.divide(priceSubtract, String.valueOf(buyTotalPrice), 2);
							s = CalculationUtil.add(s,scale);
							mtp = CalculationUtil.add(mtp,marktTotalPrice);
							buyp = CalculationUtil.add(buyp , String.valueOf(buyTotalPrice));
						}

					}
					CountDto countDO  = new CountDto();
					countDO.setBuyTotalPrice(new BigDecimal(buyp));
					countDO.setMarktTotalPrice(new BigDecimal(mtp));
					countDO.setTotalSacle(new BigDecimal(s));
					String profit  = CalculationUtil.subtract(mtp,buyp,2);
					countDO.setUserName(userDO.getUsername());
					countDO.setProfit(new BigDecimal(profit));
					list.add(countDO);
				}
			}catch (Exception e ){
				e.printStackTrace();
			}
		}
		
		List<CountDto> list = new ArrayList<>();
		for(int i = 0; i < 6; i++) {
			CountDto countDO  = new CountDto();
			countDO.setBuyTotalPrice(new BigDecimal(2));
			countDO.setMarktTotalPrice(new BigDecimal(2));
			countDO.setTotalSacle(new BigDecimal(2));
			String profit  = CalculationUtil.subtract("2","2",2);
			countDO.setUserName("小动"+i);
			countDO.setProfit(new BigDecimal(profit));
			list.add(countDO);
		}
		
		int total  = 1 ;
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}



	@GetMapping("/add")
	@RequiresPermissions("timing:combCount:add")
	String add(){
	    return "timing/combCount/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("timing:combCount:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		CombCountDO combCount = combCountService.get(id);
		model.addAttribute("combCount", combCount);
	    return "timing/combCount/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("timing:combCount:add")
	public R save( CombCountDO combCount){
		if(combCountService.save(combCount)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("timing:combCount:edit")
	public R update( CombCountDO combCount){
		combCountService.update(combCount);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("timing:combCount:remove")
	public R remove( Integer id){
		if(combCountService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("timing:combCount:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		combCountService.batchRemove(ids);
		return R.ok();
	}
	
}
